package com.example.codeEpisode;

import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
     * 流量订单Demo
     *
     * @ClassName FlowOrderDemo
     * @Description
     * @Date 2022/9/2 15:10
     **/
    public class FlowOrderDemo {
        public static String orderId = "O23020214130493945227" ;
        public static String pwd = "liuzihao" ;
        public static String proxyHost = "flow.hailiangip.com" ;
        public static int proxyPort = 14223;
        public static String destUrl = "http://zygh.czs.gov.cn/uploadfiles/202302/20230203095636659_s.png" ;
        public static String createPwd() {
            String passwordTemplate = "pwd={pwd}&pid=-1&cid=-1&uid=&sip=0&nd=0" ;
            return passwordTemplate.replace("{pwd}", pwd);
        }
        public static String httpProxyWithPassRequest(String proxyUserName, String proxyPassword) throws IOException {
            CredentialsProvider provider = new BasicCredentialsProvider();
            HttpHost host = new HttpHost(proxyHost, proxyPort);
            provider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(proxyUserName, proxyPassword));
            CloseableHttpClient client = HttpClients.custom()
                    .setDefaultCredentialsProvider(provider).build();
            HttpGet httpGet = new HttpGet(destUrl);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)
                    .setSocketTimeout(6000).setProxy(host).build();
            httpGet.setConfig(requestConfig);
            HttpResponse httpResponse = client.execute(httpGet);
            InputStream content = httpResponse.getEntity().getContent();
            File file = new File("D:\\a.png");
            org.apache.commons.io.FileUtils.copyInputStreamToFile(content, file);

            return EntityUtils.toString(httpResponse.getEntity(),"GBK");
        }
        public static void main(String[] args) {
            try {
                httpProxyWithPassRequest(orderId, createPwd());
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }